require(['../config'], function () {
    require(['jquery','_global'], function ($,_gl) {
        $(function () {
            login();
            resetpwd();
        })

        function login() {
            $("#btnlogin").click(function () {
                var $info = $("#info");
                $info.text('');
                var name = $("#username").val();
                var pwd = $("#password").val();
                if (name == "" || pwd == "") {
                    $info.text("用户名或密码不能为空！");
                    return;
                }
                var post = $.post("/Member/Login", $('#loginForm').serialize(), function (data) {
                    if (data.success) {
                        var msg = data.message;
                        window.location.href = "/Member/Index";
                    }
                    $info.text(data.message);
                });
                post.error(function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("login error：" + "status=" + XMLHttpRequest.status + " && text=" + textStatus);
                });
            });
        }

        function resetpwd() {
            $("#btnValidateCode").click(function () {
                var $this = $("this");
                var $info = $("#info");
                $info.text('');
                var mobile = $("#usermobile").val();
                if (mobile.length < 1) {
                    $info.text('请输入您的手机号码');
                    return false;
                }
                else if (!mobile.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/)) {
                    $info.text('您输入的手机号码非法');
                    return false;
                }

                var MAXTIME = 60;
                var maxTime = MAXTIME;
                var tHtml = $this.html();
                window.getPhoneVerificationCodeTimer = setInterval(function () {
                    if (maxTime <= 0) {
                        clearInterval(window.getPhoneVerificationCodeTimer);
                        maxTime = MAXTIME
                        $this.html(tHtml);
                        $this.prop("disabled", false);
                    } else {
                        maxTime--;
                        $this.html(maxTime + "秒后再发送");
                        $this.prop("disabled", true);
                    }
                }, 1000);

                var url = _gl.getAjaxUrl().sendVerifCode;
                var post = $.post(url, { mobile: mobile, opt: 2 }, function (data) {
                    if (data.success) {
                        var msg = data.message;

                    }
                    else {

                    }
                    $info.text(data.message);
                });
                post.error(function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("SendVerifCode error：" + "status=" + XMLHttpRequest.status + " && text=" + textStatus);
                });
            });
            $("#btnUpdatePassword").click(function () {
                var $info = $("#info");
                $info.text('');
                var mobile = $("#usermobile").val();
                var vcode = $("#uservcode").val();
                var newpwd = $("#newpassword").val();
                if (mobile == "") {
                    $info.text("导购员手机号码不能为空！");
                    return;
                }
                if (vcode == "") {
                    $info.text("导购员验证码不能为空！");
                    return;
                }
                if (newpwd == "") {
                    $info.text("导购员新密码不能为空！");
                    return;
                }
                if (newpwd.length < 6) {
                    $info.text("密码长度必须大于等于6位！");
                    return;
                }
                var post = $.post("/Member/ResetPwd", $('#resetPwdForm').serialize(), function (data) {
                    if (data.success) {
                        var msg = data.message;
                        window.location.href = "/Member/Index";
                    }
                    $info.text(data.message);
                });
                post.error(function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("resetpwd error：" + "status=" + XMLHttpRequest.status + " && text=" + textStatus);
                });
            });
        }
    });
});